package com.uwlaser.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.uwlaser.domain.*;
import com.uwlaser.service.PlanService;
import com.uwlaser.service.dto.GetWorkOrderParam;
import com.uwlaser.service.dto.GetWorkOrderResult;
import com.uwlaser.service.mapper.PlanMixedLineMapper;
import com.uwlaser.service.mapper.PlanMixedRuleMapper;
import com.uwlaser.service.mapper.PlanWorkOrderMapper;
import com.uwlaser.service.mapper.ProductModelMapper;
import com.uwlaser.utils.StringUtils;
import com.uwlaser.utils.enums.DictValueEnum;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
* @author zk-Qin
* @date 2022-05-11
*/
@Service
@AllArgsConstructor
@Transactional(propagation = Propagation.SUPPORTS, readOnly = true, rollbackFor = Exception.class)
public class PlanServiceImpl implements PlanService {

    //工单
    private final PlanWorkOrderMapper planWorkOrderMapper;

    //混线
    private final PlanMixedLineMapper planMixedLineMapper;

    //混线规则
    private final PlanMixedRuleMapper planMixedRuleMapper;

    //产品型号
    private final ProductModelMapper productModelMapper;

    /**
     * 获取工单信息
     * @param requestParam
     * @return
     */
    @Override
    public GetWorkOrderResult getWorkOrder(RequestParam<GetWorkOrderParam> requestParam) {
        //请求参数
        GetWorkOrderParam param = requestParam.getParam();
        //返回参数
        GetWorkOrderResult result = new GetWorkOrderResult();

        PlanWorkOrder workOrder = null;

        if(StringUtils.isEmpty(param.getWorkOrderCode())){
            //获取当前工段正在生产的工单
            workOrder = getCurrentWorkOrder(requestParam.getFactoryLine().getModelCode(),requestParam.getFactorySection().getModelCode());
        }else{
            workOrder = getWorkOrderByCode(param.getWorkOrderCode());
        }

        //获取产品信息
        ProductModel productModel = getProductModelByCode(workOrder.getProductCode());

        //设置值
        result.setProduct(productModel.getProductType(),productModel.getProductCode(),productModel.getProductName());
        result.setWorkOrder(
                workOrder.getWorkOrderCode(),
                workOrder.getLineCode(),
                workOrder.getSectionCode(),
                workOrder.getOrderStatus(),
                workOrder.getRequiredQty(),
                workOrder.getFinishQty(),
                workOrder.getFailQty(),
                workOrder.getPlanStart()
                ,workOrder.getPlanEnd());
        return result;
    }


    /**
     * 根据工段获取当前正在生产的工单信息
     *  * @param lienCode
     * @param sectionCode
     * @return
     */
    private PlanWorkOrder getCurrentWorkOrder(String lienCode, String sectionCode) {
        //获取当前工段启用的混线规则
        PlanMixedLine planMixedLine = new PlanMixedLine();
        planMixedLine.setLineCode(lienCode);
        planMixedLine.setSectionCode(sectionCode);
        planMixedLine.setIsDisable(DictValueEnum.START.getValue());
        PlanMixedLine mixedLine = planMixedLineMapper.selectOne(new QueryWrapper<>(planMixedLine));
        if(mixedLine != null){
            //获取当前正在循环的产品
            PlanMixedRule planMixedRule = new PlanMixedRule();
            planMixedRule.setMixedLineId(mixedLine.getId());
            planMixedRule.setInProcess(DictValueEnum.YES.getValue());
            PlanMixedRule mixedRule = planMixedRuleMapper.selectOne(new QueryWrapper<>(planMixedRule));
            if(mixedRule != null){
                //获取当前正在循环的产品工单
                PlanWorkOrder planWorkOrder = new PlanWorkOrder();
                planWorkOrder.setProductCode(mixedRule.getProductCode());
                planWorkOrder.setLineCode(lienCode);
                planWorkOrder.setSectionCode(sectionCode);
                planWorkOrder.setOrderStatus(DictValueEnum.WORK_STATUS_BEGIN.getValue());
                PlanWorkOrder workOrder = planWorkOrderMapper.selectOne(new QueryWrapper<>(planWorkOrder));
                if(workOrder != null){
                    return workOrder;
                }
            }
        }

        //获取
        PlanWorkOrder planWorkOrder = new PlanWorkOrder();
        planWorkOrder.setLineCode(lienCode);
        planWorkOrder.setSectionCode(sectionCode);
        planWorkOrder.setOrderStatus(DictValueEnum.WORK_STATUS_BEGIN.getValue());
        List<PlanWorkOrder> planWorkOrders = planWorkOrderMapper.selectList(new QueryWrapper<>(planWorkOrder));
        if(planWorkOrders == null || planWorkOrders.size() == 0){
            throw new RuntimeException("当前工段【"+sectionCode+"】没有生产的工单！请下发新工单后再试！");
        }

        return planWorkOrders.get(0);
    }

    /**
     * 根据工单编号获取工单信息
     * @param workOrderCode
     * @return
     */
    private PlanWorkOrder getWorkOrderByCode(String workOrderCode) {
        PlanWorkOrder planWorkOrder = new PlanWorkOrder();
        planWorkOrder.setWorkOrderCode(workOrderCode);
        PlanWorkOrder workOrder = planWorkOrderMapper.selectOne(new QueryWrapper<>(planWorkOrder));
        if(workOrder == null){
            throw new RuntimeException("获取工单【"+workOrderCode+"】失败！上传的工单编号错误或者工单已经被删除，获取失败！");
        }
        return workOrder;
    }

    /**
     * 根据产品型号获取产品信息
     * @param productCode
     * @return
     */
    private ProductModel getProductModelByCode(String productCode){
        ProductModel productModel = new ProductModel();
        productModel.setProductCode(productCode);
        ProductModel model = productModelMapper.selectOne(new QueryWrapper<>(productModel));
        if(model == null){
           throw new RuntimeException("获取产品型号【"+productCode+"】失败,该产品型号已被删除。");
        }
        return model;
    }
}

